package com.white.algebra.controller;

import com.white.algebra.domain.Permission;
import com.white.algebra.domain.Role;
import com.white.algebra.service.PermissionService;
import com.white.algebra.service.RoleService;
import com.white.algebra.service.impl.PermissionServiceImpl;
import com.white.algebra.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class PermissionController extends BaseServlet{
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        PermissionService permissionService=new PermissionServiceImpl();
        List<Permission> permission = permissionService.findAll();
        req.setAttribute("permissionList",permission);
        req.getRequestDispatcher("/pages/permission-list.jsp").forward(req,resp);
    }
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        PermissionService permissionService=new PermissionServiceImpl();
        RoleService roleService=new RoleServiceImpl();
        String id=req.getParameter("id");
        List<Role> roles = roleService.findRoleByPermissionId(id);
        req.setAttribute("role",roles);
        Permission permission = permissionService.findPermissionById(id);
        req.setAttribute("permission",permission);
        req.getRequestDispatcher("/pages/permission-show.jsp").forward(req,resp);
    }
    protected void addRoleByPermissionId(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        PermissionService permissionService=new PermissionServiceImpl();
        String permissionId=req.getParameter("permissionId");
        String roleId=req.getParameter("roleId");
        permissionService.addRoleByPermissionId(permissionId,roleId);

        resp.sendRedirect(req.getHeader("Referer"));
    }
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        PermissionService permissionService=new PermissionServiceImpl();
        String permissionName=req.getParameter("permissionName");
        String url=req.getParameter("url");
        Permission permission=new Permission();
        permission.setPermissionName(permissionName);
        permission.setUrl(url);
        permissionService.save(permission);
        resp.sendRedirect(req.getContextPath()+"/permission?action=findAll");
    }
    protected void deletePermissionById(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        PermissionService permissionService=new PermissionServiceImpl();
        String id=req.getParameter("permissionId");
        permissionService.deleteRole_PermissionByPermissionId(id);
        resp.sendRedirect(req.getContextPath()+"/permission?action=findAll");
    }
    protected void deleteRoleById(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        PermissionService permissionService=new PermissionServiceImpl();
        String permissionId=req.getParameter("permissionId");
        String roleId=req.getParameter("roleId");
        permissionService.deleteRoleById(permissionId,roleId);
        resp.sendRedirect(req.getHeader("Referer"));
    }
}
